package cn.com.aiton.common.utils;

import cn.com.aiton.common.model.ServiceResult;
import cn.com.aiton.common.model.gbt209992007.GbtOverlapPhaseDomain;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class JsonToOverlapPhaseUtil {
    private static final Logger LOG = LoggerFactory.getLogger(JsonToOverlapPhaseUtil.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();
    public static ServiceResult<List<GbtOverlapPhaseDomain>> json2OverlapPhaseList(String json){
        ServiceResult<List<GbtOverlapPhaseDomain>> map = new ServiceResult<>();
        try {
            List<GbtOverlapPhaseDomain> listll = objectMapper.readValue(json, new TypeReference<List<GbtOverlapPhaseDomain>>(){});
            map.setSuccess(true);
            map.setObject(listll);
            return map;
        }catch (Exception e){
            LOG.error(e.getMessage(),e);
            map.setSuccess(false);
            map.setMessage("数据转换出错");
            return map;
        }
    }

    private static int listToInt(List<Integer> list){
        int i = 0;
        for(int j:list){
            i = i | (1<<(j));
        }
        return i;
    }

    private static List<Integer> objectToList(JSONArray array) throws JSONException {
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<array.length();i++){
            list.add(array.getInt(i));
        }
        return list;
    }
}
